package cn.bigcontainer.alt.meta.controller;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.bigcontainer.alt.common.exception.BigResultCode;
import cn.bigcontainer.alt.meta.po.MButton;
import cn.bigcontainer.alt.meta.service.MetaBizService;

/**
 * 业务视图元数据管理Controller
 * 
 * @author jiaod
 *
 */
@Controller
@RequestMapping("/meta/man/biz")
public class MetaBizController {

	@Resource
	private MetaBizService metaBizService;

	/**
	 * @param app
	 * @param viewproperty
	 * @param offset       小于0表示前移，大于0表示后移
	 * @return
	 */
	@RequestMapping("/viewproperty/move")
	@ResponseBody
	public BigResultCode moveViewProperty(String app, String viewproperty, Integer offset) {
		metaBizService.moveViewProperty(app, viewproperty, offset);
		return BigResultCode.getSuccessCode();
	}

	@RequestMapping("/viewproperty/hide")
	@ResponseBody
	public BigResultCode hideViewProperty(String app, String viewproperty) {
		metaBizService.hideViewProperty(app, viewproperty);
		return BigResultCode.getSuccessCode();
	}

	@RequestMapping("/viewproperty/show")
	@ResponseBody
	public BigResultCode showViewProperty(String app, String viewproperty, String hidden) {
		metaBizService.showViewProperty(app, viewproperty, hidden);
		return BigResultCode.getSuccessCode();
	}

	@RequestMapping("/viewproperty/delete")
	@ResponseBody
	public BigResultCode deleteViewProperty(String app, String viewproperty, Boolean cascade) {
		metaBizService.deleteViewProperty(app, viewproperty, cascade);
		return BigResultCode.getSuccessCode();
	}

	/**
	 * *设置尺寸
	 * 
	 * @param app
	 * @param viewproperty
	 * @param width
	 * @param height
	 * @return
	 */
	@RequestMapping("/viewproperty/resize")
	@ResponseBody
	public BigResultCode resizeViewProperty(String app, String viewproperty,
			@RequestParam(defaultValue = "-1") Integer width, @RequestParam(defaultValue = "-1") Integer height) {
		metaBizService.resizeViewProperty(app, viewproperty, width, height);
		return BigResultCode.getSuccessCode();
	}

	@RequestMapping("/viewproperty/order")
	@ResponseBody
	public BigResultCode orderViewProperty(String app, String viewproperty, String ordertype) {
		metaBizService.orderViewProperty(app, viewproperty, ordertype);
		return BigResultCode.getSuccessCode();
	}

	/**
	 * *切换排序
	 * 
	 * @param app
	 * @param viewproperty
	 * @return
	 */
	@RequestMapping("/viewproperty/toggleorder")
	@ResponseBody
	public BigResultCode toggleOrderViewProperty(String app, String viewproperty) {
		metaBizService.toggleOrderViewProperty(app, viewproperty);
		return BigResultCode.getSuccessCode();
	}

	/**
	 * *取消排序
	 * 
	 * @param app
	 * @param viewproperty
	 * @return
	 */
	@RequestMapping("/viewproperty/unorder")
	@ResponseBody
	public BigResultCode unorderViewProperty(String app, String viewproperty) {
		metaBizService.unorderViewProperty(app, viewproperty);
		return BigResultCode.getSuccessCode();
	}

	/**
	 * *加载视图属性
	 * 
	 * @param app
	 * @param viewproperty
	 * @return
	 */
	@RequestMapping("/viewproperty/load")
	@ResponseBody
	public BigResultCode loadViewProperty(String app, String viewproperty) {
		Map<String, Object> data = metaBizService.loadViewProperty(app, viewproperty);
		return BigResultCode.getSuccessCode().setContent(data);
	}

	/**
	 * 修改视图属性
	 * 
	 * @param app
	 * @param viewproperty
	 * @return
	 */
	@RequestMapping("/viewproperty/update")
	@ResponseBody
	public BigResultCode updateViewProperty(String app, @RequestParam("id") String viewproperty,
			@RequestParam Map<String, Object> req) {
		metaBizService.updateViewProperty(app, viewproperty, req);
		return BigResultCode.getSuccessCode();
	}

	/**
	 * 加载视图属性
	 * 
	 * @param app
	 * @param viewproperty
	 * @return
	 */
	@RequestMapping("/viewproperty/createproperty")
	@ResponseBody
	public BigResultCode createViewProperty(String app, String viewproperty, Boolean select,
			@RequestParam Map<String, Object> req) {
		metaBizService.createViewProperty(app, viewproperty, select, req);
		return BigResultCode.getSuccessCode();
	}

	/**
	 * 加载视图属性
	 * 
	 * @param app
	 * @param viewproperty
	 * @return
	 */
	@RequestMapping("/viewproperty/createdictionary")
	@ResponseBody
	public BigResultCode createViewPropertyByDictionary(String app, String viewproperty,
			@RequestParam Map<String, Object> req) {
		metaBizService.createViewPropertyByDictionary(app, viewproperty, req);
		return BigResultCode.getSuccessCode();
	}

	/**
	 * @param app
	 * @param viewproperty
	 * @param req
	 * @return
	 */
	@RequestMapping("/viewproperty/createrelation")
	@ResponseBody
	public BigResultCode createViewPropertyByRelation(String app, String viewproperty,
			@RequestParam Map<String, Object> req) {
		metaBizService.createViewPropertyByRelation(app, viewproperty, req);
		return BigResultCode.getSuccessCode();
	}

	/**
	 * 取消所有排序
	 * 
	 * @param app
	 * @param view
	 * @return
	 */
	@RequestMapping("/view/unorder")
	@ResponseBody
	public BigResultCode unorderView(String app, String view) {
		metaBizService.unorderView(app, view);
		return BigResultCode.getSuccessCode();
	}
	
	@RequestMapping("/list/setrows")
	@ResponseBody
	public BigResultCode setRows(String list, Integer rows) {
		metaBizService.setListRows(list, rows);
		return BigResultCode.getSuccessCode();
	}
	
	/**
	 * 取消分页
	 * @param list
	 * @param rows
	 * @return
	 */
	@RequestMapping("/list/unpage")
	@ResponseBody
	public BigResultCode unpage(String list) {
		metaBizService.unpage(list);
		return BigResultCode.getSuccessCode();
	}

	/**
	 * inputor选择MObject时的查询和数据接口
	 * 
	 * @param searchContent 值不为空表示模糊查询对象的名称、编号、id
	 * @param keyValue      对象的id，多个以都好隔开，格式为：m_object:1,m_object:2，其中1和2为m_object表中的id
	 * @return
	 */
	@RequestMapping("/object")
	@ResponseBody
	public BigResultCode searchObject(String searchContent, String keyValue) {
		List<Map<String, Object>> list = metaBizService.searchObject(searchContent, keyValue);
		return BigResultCode.getSuccessCode().setContent(list);
	}

	/**
	 * combo选择关系时的查询和数据接口
	 * 
	 * @param object
	 * @param relation
	 * @return
	 */
	@RequestMapping("/relation")
	@ResponseBody
	public BigResultCode searchRelation(String object) {
		List<Map<String, Object>> list = metaBizService.searchRelation(object);
		return BigResultCode.getSuccessCode().setContent(list);
	}

	/**
	 * combo选择属性时的查询和数据接口
	 * 
	 * @param object
	 * @param relation
	 * @return
	 */
	@RequestMapping("/property")
	@ResponseBody
	public BigResultCode searchProperty(String object, String relation) {
		List<Map<String, Object>> list = metaBizService.searchProperty(object, relation);
		return BigResultCode.getSuccessCode().setContent(list);
	}

	/**
	 * 在应用app的button之后创建按钮
	 * 
	 * @param app
	 * @param buttonview
	 * @param button
	 * @param mButton
	 * @return
	 */
	@RequestMapping("/button/create")
	@ResponseBody
	public BigResultCode createButton(String app, String buttonview, String button, MButton mButton) {
		metaBizService.createButton(app, buttonview, button, mButton);
		return BigResultCode.getSuccessCode();
	}

	/**
	 * 將应用app的button前移或者后移
	 * 
	 * @param app
	 * @param button
	 * @param offset
	 * @return
	 */
	@RequestMapping("/button/move")
	@ResponseBody
	public BigResultCode moveButton(String app, String button, Integer offset) {
		metaBizService.moveButton(app, button, offset);
		return BigResultCode.getSuccessCode();
	}

	/**
	 * 删除按钮
	 * 
	 * @param app
	 * @param button
	 * @return
	 */
	@RequestMapping("/button/delete")
	@ResponseBody
	public BigResultCode deleteButton(String app, String button) {
		metaBizService.deleteButton(app, button);
		return BigResultCode.getSuccessCode();
	}
}
